package com.neusoft.elmboot.exception;

import com.neusoft.elmboot.exception.exceptions.BaseException;
import com.neusoft.elmboot.exception.exceptions.IllegalAmountException;
import com.neusoft.elmboot.util.response.CodeEnum;
import com.neusoft.elmboot.util.response.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler({Exception.class})
    public ResultBody returnException(Exception e) {
        log.error("Exception", e);
        return ResultBody.error(CodeEnum.UNKNOWN_ERROR);
    }

    @ExceptionHandler(RuntimeException.class)
    public ResultBody returnRuntimeException(Exception e) {
        log.warn("RuntimeException", e);
        return ResultBody.error(CodeEnum.FAILED);
    }

    @ExceptionHandler(BaseException.class)
    public ResultBody returnBaseException(BaseException e) {
        return e.getResult();
    }
}
